package com.ruoyi.lipin.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lipin.domain.vo.LpCollectionVo;
import com.ruoyi.lipin.domain.bo.LpCollectionBo;
import com.ruoyi.lipin.service.ILpCollectionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户收藏
 *
 * @author ruoyi
 * @date 2025-03-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lipin/collection")
public class LpCollectionController extends BaseController {

    private final ILpCollectionService iLpCollectionService;

    /**
     * 查询用户收藏列表
     */
    @SaCheckPermission("lipin:collection:list")
    @GetMapping("/list")
    public TableDataInfo<LpCollectionVo> list(LpCollectionBo bo, PageQuery pageQuery) {
        return iLpCollectionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户收藏列表
     */
    @SaCheckPermission("lipin:collection:export")
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LpCollectionBo bo, HttpServletResponse response) {
        List<LpCollectionVo> list = iLpCollectionService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户收藏", LpCollectionVo.class, response);
    }

    /**
     * 获取用户收藏详细信息
     *
     * @param collectionId 主键
     */
    @SaCheckPermission("lipin:collection:query")
    @GetMapping("/{collectionId}")
    public R<LpCollectionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long collectionId) {
        return R.ok(iLpCollectionService.queryById(collectionId));
    }

    /**
     * 新增用户收藏
     */
    @SaCheckPermission("lipin:collection:add")
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LpCollectionBo bo) {
        return toAjax(iLpCollectionService.insertByBo(bo));
    }

    /**
     * 修改用户收藏
     */
    @SaCheckPermission("lipin:collection:edit")
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LpCollectionBo bo) {
        return toAjax(iLpCollectionService.updateByBo(bo));
    }

    /**
     * 删除用户收藏
     *
     * @param collectionIds 主键串
     */
    @SaCheckPermission("lipin:collection:remove")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{collectionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] collectionIds) {
        return toAjax(iLpCollectionService.deleteWithValidByIds(Arrays.asList(collectionIds), true));
    }
}
